package org.sgx.gapi.client.apis.pagespeed;

import org.sgx.gapi.client.GAPI;
import org.sgx.gapi.client.client.ClientRequest;
import org.sgx.gapi.client.client.ClientRequestCallback;
import org.sgx.gapi.client.client.GAPICallback;
import org.sgx.gapi.client.client.GAPIRequest;
import org.sgx.gapi.client.client.GAPIResult;
import org.sgx.gapi.client.client.HttpRequest;
import org.sgx.jsutil.client.JsObject;

/**
 * @see https://developers.google.com/speed/docs/insights/v1/reference
 * @author sg
 * 
 */
public class PageSpeedGetRequest implements GAPIRequest<PageSpeedResult> {

	String url, userIp, locale, strategy, rule;
	
	public PageSpeedGetRequest(String url) {
		this.url=url;
	}
	@Override
	public void execute(final GAPICallback<PageSpeedResult> c) {
		JsObject params = JsObject.createObject().cast(); 
		if(getUrl()!=null)
			params.objPut("url", getUrl());
		if(getLocale()!=null)
			params.objPut("locale", getLocale());
		if(getStrategy()!=null)
			params.objPut("strategy", getStrategy());
		ClientRequest clientReq = ClientRequest.create()
			.path("/pagespeedonline/v1/runPagespeed").params(params);
		HttpRequest req = GAPI.get().client().request(clientReq);
		req.execute(new ClientRequestCallback() {
			@Override
			public void call(GAPIResult jsonResp, String rawResp) {
				c.call((PageSpeedResult) jsonResp);
			}
		});
	}
	
	/**
	 * The URL of the page for which the PageSpeed Insights API should generate results.
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * The URL of the page for which the PageSpeed Insights API should generate results.
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	/**
	 * IP address of the site where the request originates.  Use this if you want to enforce per-user limits.
	 * @return
	 */
	public String getUserIp() {
		return userIp;
	}
	/**
	 * IP address of the site where the request originates.  Use this if you want to enforce per-user limits.
	 * @param userIp
	 */
	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}
	/**
	 * The locale that results should be generated in. See the list of supported locales. If the specified locale is not supported, the default locale is used.
	 * @return
	 */
	public String getLocale() {
		return locale;
	}
	/**
	 * The locale that results should be generated in. See the list of supported locales. If the specified locale is not supported, the default locale is used.
	 * @param locale
	 */
	public void setLocale(String locale) {
		this.locale = locale;
	}
	/**
	 * The strategy to use when analyzing the page. Valid values are desktop and mobile.
	 * @return
	 */
	public String getStrategy() {
		return strategy;
	}
	/**
	 * The strategy to use when analyzing the page. Valid values are desktop and mobile.
	 * @param strategy
	 */
	public void setStrategy(String strategy) {
		this.strategy = strategy;
	}
	/**
	 * The PageSpeed rules to run. Can be specified multiple times (for example, &rule=AvoidBadRequests&rule=MinifyJavaScript) to request multiple rules. If unspecified, all rules for the current strategy are used. Most users of the API should not need to specify this parameter.
	 * @return
	 */
	public String getRule() {
		return rule;
	}
	/**
	 * The PageSpeed rules to run. Can be specified multiple times (for example, &rule=AvoidBadRequests&rule=MinifyJavaScript) to request multiple rules. If unspecified, all rules for the current strategy are used. Most users of the API should not need to specify this parameter.
	 * @param rule
	 */
	public void setRule(String rule) {
		this.rule = rule;
	}

	
	
}
